package com.zxjbyte.yiyi.module.system.controller.dict;

import com.zxjbyte.yiyi.framework.common.constant.Constant;
import com.zxjbyte.yiyi.framework.common.domain.Kv;
import com.zxjbyte.yiyi.framework.common.domain.api.PageResult;
import com.zxjbyte.yiyi.framework.common.domain.api.R;
import com.zxjbyte.yiyi.framework.common.util.CollXUtil;
import com.zxjbyte.yiyi.module.system.domain.entity.DictDataPO;
import com.zxjbyte.yiyi.module.system.domain.entity.DictTypePO;
import com.zxjbyte.yiyi.module.system.domain.request.dict.*;
import com.zxjbyte.yiyi.module.system.domain.response.dict.DictDataDetailResponse;
import com.zxjbyte.yiyi.module.system.domain.response.dict.DictDataPageResponse;
import com.zxjbyte.yiyi.module.system.domain.response.dict.DictTypeDetailResponse;
import com.zxjbyte.yiyi.module.system.domain.response.dict.DictTypePageResponse;
import com.zxjbyte.yiyi.module.system.manager.DictManager;
import com.zxjbyte.yiyi.module.system.service.dict.IDictDataService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author zhangxingjia
 * @Date 2024/1/24 16:14
 * @Version: 1.0
 */
@Slf4j
@Tag(name = "字典数据")
@RestController
@RequestMapping(value = Constant.BASE_PATH_B + "/system/dictdata")
public class DictDataController {

    @Resource
    private IDictDataService dictDataService;
    @Resource
    private DictManager dictManager;

    @Operation(summary = "字典数据分页查询")
    @GetMapping("/page")
    public PageResult<DictDataPageResponse> page(@ParameterObject DictDataPageRequest request){
        return dictDataService.page(request);
    }

    @Operation(summary = "字典数据详情查询")
    @GetMapping("/detail")
    public R<DictDataDetailResponse> detail(@Parameter(description = "唯一id" ) @RequestParam Long id){
        return R.data(dictDataService.detail(id));
    }

    @Operation(summary = "新增字典数据")
    @PostMapping("/add")
    public R<?> add(@Valid @RequestBody DictDataAddRequest request){
        dictManager.dictDataAdd(request);
        return R.ok();
    }

    @Operation(summary = "编辑字典数据")
    @PutMapping("/update")
    public R<?> update(@Valid @RequestBody DictDataUpdateRequest request){
        dictManager.dictDataUpdate(request);
        return R.ok();
    }

    @Operation(summary = "删除字典数据")
    @DeleteMapping("/del")
    public R<?> delete(@Parameter(description = "唯一id" ) @RequestParam Long id){
        dictManager.dictDataDelete(id);
        return R.ok();
    }

    @Operation(summary = "字典数据下拉框")
    @GetMapping("/dropdown")
    public R<List<Kv>> dropdown(@Parameter(description = "字典类型") @RequestParam String dictType){
        List<DictDataPO> list = dictDataService.getByDictType(dictType);
        if(CollXUtil.isNotEmpty(list)){
            List<Kv> resultList = list.stream().map(s -> Kv.create().set("value", s.getDictValue()).set("label", s.getDictLabel()))
                    .collect(Collectors.toList());
            return R.data(resultList);
        }
        return R.ok();
    }
}
